﻿/*
 * (c) Stefan Bojarovski 2010
 * */
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASTBuilder
{
    class BlockNode : NodesCollection, ITreeNode
    {
        private LexemeType lexType = LexemeType.BLOCK;

        private List<ITreeNode> blockComponents;

        public BlockNode()
        {
            blockComponents = new List<ITreeNode>();
        }
        public void addStatement(ITreeNode statement)
        {
            blockComponents.Add(statement);
        }
        public LexemeType getLexemeType()
        {
            return lexType;
        }
        public void printNode(String parentLevel, StreamWriter someStream)
        {
            someStream.WriteLine(parentLevel + " " + "Block: ");
            foreach (ITreeNode blockComp in blockComponents)
            {
                blockComp.printNode(parentLevel + " ", someStream);
            }
        }
        public List<Command> evalNode(ref Direction next, 
                                        ref Dictionary<String, Pair< IdentifierType, object>> Environment)
        {
            List<Command> block = new List<Command>();
            if (next != Direction.JUMP)
            {
                foreach (ITreeNode component in blockComponents)
                {
                    Command temp = component.evalNode(ref next, ref Environment)[0];
                    block.Add(temp);
                }
                return block;
            }
            else
            {
                block.Add(new Command(CommandType.VOID, null, null));
                return block;
            }
        }
    }
}